package com.upstairstechnology.oi.determination;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Defines an integration check for oi to do.
 * 
 * @author Jeff Jensen
 */
public class IntegrationCheck implements Comparable<IntegrationCheck> {
    /** The specified branchspec to use for checking an integration need. */
    private String branchspec;

    // must place target before source for compareTo()'s reflection
    // implementation.
    /**
     * The name of the destination codeline to compare to the source codeline
     * for outstanding integrations. Outstanding integrations are merged into
     * this codeline.
     */
    private String targetCodeline;

    /**
     * The name of the starting codeline to compare to the target codeline for
     * outstanding integrations. Outstanding integrations are merged from this
     * codeline.
     */
    private String sourceCodeline;

    /**
     * Returns true if a branchspec is defined for this integration check.
     * 
     * @return true if a branchspec is defined for this integration check, and
     *         false if null or empty.
     */
    public boolean hasBranchspec() {
        boolean hasValue = false; // NOPMD

        if (branchspec != null && !branchspec.isEmpty()) {
            hasValue = true;
        }

        return hasValue;
    }

    /**
     * Returns true if a source and target codeline pair is defined for this
     * integration check.
     * 
     * @return true if a source and target codeline pair is defined for this
     *         integration check, and false if null or empty.
     */
    public boolean hasSourceAndTarget() {
        boolean hasValue = false; // NOPMD

        if (sourceCodeline != null && !sourceCodeline.isEmpty()
                && targetCodeline != null && !targetCodeline.isEmpty()) {
            hasValue = true;
        }

        return hasValue;
    }

    /**
     * {@inheritDoc}
     */
    public int compareTo(IntegrationCheck o) {
        int comparedValue = CompareToBuilder.reflectionCompare(this, o);
        return comparedValue;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        boolean isEqual = EqualsBuilder.reflectionEquals(this, o);
        return isEqual;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        int hc = HashCodeBuilder.reflectionHashCode(this);
        return hc;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    /**
     * Get the branchspec name that defines the integration for the integration.
     * Not all SCMs have a branchspec feature.
     * 
     * @return The branchspec name.
     */
    public String getBranchspec() {
        return branchspec;
    }

    /**
     * Get the source codeline name for the integration.
     * 
     * @return The source codeline name for the integration.
     */
    public String getSourceCodeline() {
        return sourceCodeline;
    }

    /**
     * Get the target codeline name for the integration.
     * 
     * @return The target codeline name for the integration.
     */
    public String getTargetCodeline() {
        return targetCodeline;
    }

    /**
     * Set the branchspec name that defines the integration.
     * 
     * @param name
     *            The branchspec name for the integration.
     */
    public void setBranchspec(String name) {
        this.branchspec = name;
    }

    /**
     * Set the source codeline name for the integration.
     * 
     * @param name
     *            The source codeline name for the integration.
     */
    public void setSourceCodeline(String name) {
        this.sourceCodeline = name;
    }

    /**
     * Set the target codeline name for the integration.
     * 
     * @param name
     *            The target codeline name for the integration.
     */
    public void setTargetCodeline(String name) {
        this.targetCodeline = name;
    }
}
